package lomasky.ma.httpUtils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.TextView;

import com.saifan.wyy_ov.R;

/**
 * @author Eicky
 * @Description: 请求数据加载对话框
 * @date: 2016/6/2 14:59
 * @version: V1.0
 */
public class ProgressDialogUtil {
    private static Dialog progressDialog;//进度框

    /**
     * @param content 上下文
     * @param cancel  显示的提示信息
     */
    public static void showProgressDialog(Context content, boolean cancel) {
        try {
            Activity act = (Activity) content;
            if (!act.isFinishing()) {
                progressDialog = new Dialog(content, R.style.dialog);
                progressDialog.setContentView(R.layout.progress_dialog);
                progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode,
                                         KeyEvent event) {
                        switch (keyCode) {
                            case 4:
                                dialog.dismiss();
                                break;
                            default:
                                break;
                        }
                        return true;
                    }
                });
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();


            }
        } catch (IllegalArgumentException e) {
        } catch (Exception e) {
        }
    }

    /**
     * @param context 上下文
     * @param cancel  显示的提示信息
     */
    public static void showProgressDialog(Context context, boolean cancel, String msg) {
        Activity activity = (Activity) context;
        if (!activity.isFinishing()) {
            if (progressDialog == null) {
                progressDialog = new Dialog(context, R.style.progress_dialog);

                progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode,
                                         KeyEvent event) {
                        switch (keyCode) {
                            case KeyEvent.KEYCODE_BACK:
                                dialog.dismiss();
                                break;
                            default:
                                break;
                        }
                        return true;
                    }
                });


                progressDialog.setCanceledOnTouchOutside(cancel);
                progressDialog.show();
                progressDialog.setContentView(R.layout.progress_dialog);

                if (!TextUtils.isEmpty(msg)) {
                    Window window = progressDialog.getWindow();
                    TextView textView = (TextView) window.findViewById(R.id.tip);
                    textView.setText(msg);
                }

            }

        }
    }

    /**
     * 关闭进度对话框
     */
    public static void closeProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
}
